package com.yixincaipiao.ui.main.action;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import com.android.volley.VolleyError;
import com.yixincaipiao.R;
import com.yixincaipiao.common.CommonConfig;
import com.yixincaipiao.network.FileDownload;
import com.yixincaipiao.network.Http;
import com.yixincaipiao.network.NetRequest;
import com.yixincaipiao.ui.main.MainActivity;
import com.yixincaipiao.utils.L;
import com.yixincaipiao.weight.Dialogs;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 更新版本
 */

public class CheckNewVersionAction {
    private String versionName;
    private Activity context;
    private boolean isFouceUpdate;
    //下载通知
    private Notification notification;
    private NotificationManager notificationManager;
    private int downId;//下载推送ID notification识别
    private String title = "更新客户端";//推送栏名称

    public CheckNewVersionAction(Activity context, boolean isFouceUpdate) {
        this.isFouceUpdate = isFouceUpdate;
//        this.versionName = versionName;//获取  版本信息
        this.context = context;
    }

    public void checkNewVersion() {
        String url = "http://www.geeboo.com/more/cUpdateGeebook.go";// params= {type=1, version=312}
        Map<String, Object> params = new HashMap<>();
        params.put("type", 1);
        params.put("version", "312");

        Http.aSyncPost(context, Dialogs.getWaitDialog(context), url, params, new NetRequest.NetRequestListener() {
            @Override
            public void onAccessComplete(boolean success, String object, VolleyError error, String flag) {
                L.i("版本信息       " + object);
                //假定有新版本
                //强制更新,或者选择更新
                if (isFouceUpdate) {
                    getNewVersionData("http://www.geeboo.com/librarys/client/Geeboo.apk");
                } else {
                    getNewVersionData("http://www.geeboo.com/librarys/client/Geeboo.apk");
                }
            }
        });

    }

    /**
     * 下载
     *
     * @param apkURL
     */
    public void getNewVersionData(String apkURL) {
        File file = new File(CommonConfig.PATH + "Test.apk");
        FileDownload downloadAPK = new FileDownload(apkURL, file, 0, new FileDownload.FileDownloadListener() {
            @Override
            public void onDownStart(String url) {
                downId = -99;
                Intent nullIntent = new Intent(context, MainActivity.class);//点击消息到main

                notification = new Notification();
                notification.icon = R.mipmap.ic_launcher;
                notification.tickerText = title;
                notification.when = System.currentTimeMillis();
                notification.defaults = Notification.DEFAULT_LIGHTS;
                notification.contentView = new RemoteViews(context.getPackageName(), R.layout.apk_down_notification);
                notification.contentIntent = PendingIntent.getActivity(context, 0, nullIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(downId, notification);

            }

            @Override
            public void onDownProgress(String url, float progress) {
//                progressBar.setProgress((int) (progress * 100));
                Log.e("after", "刷新刻度                " + progress);
                notification.contentView.setViewVisibility(R.id.other_view, View.GONE);
                notification.contentView.setViewVisibility(R.id.normal_view, View.VISIBLE);
                notification.contentView.setTextViewText(R.id.normal_text, title);
                notification.contentView.setTextViewText(R.id.percent_text, (int) (progress * 100) + "%");
                notification.contentView.setProgressBar(R.id.percent__process, 100, (int) (progress * 100), false);
                notificationManager.notify(downId, notification);
            }

            @Override
            public void onDownEnd(String url) {
//                progressBar.setProgress(100);
                notification.contentView.setViewVisibility(R.id.other_view, View.VISIBLE);
                notification.contentView.setViewVisibility(R.id.normal_view, View.GONE);
                notification.contentView.setTextViewText(R.id.other_text, title);
                notification.contentView.setTextViewText(R.id.other_operate, "下载完成");
//                if(runIntent){
//                    notification.contentView.setOnClickPendingIntent(R.id.other_view,PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT));
//                } else {
                notificationManager.cancel(downId);//下载完成直接消除
//                    return;
//                }
                notificationManager.notify(downId, notification);
                //去安装
                context.startActivity(getInstallApk());

            }

            @Override
            public void onDownError(String err) {
                notification.contentView.setViewVisibility(R.id.other_view, View.VISIBLE);
                notification.contentView.setViewVisibility(R.id.normal_view, View.GONE);
                notification.contentView.setTextViewText(R.id.other_text, title);
                notification.contentView.setTextViewText(R.id.other_operate, "下载出错");
                notificationManager.notify(downId, notification);
            }
        });
        downloadAPK.doIt();
    }

    /**
     * 安装
     *
     * @return
     */
    private Intent getInstallApk() {
        File localApk = new File(CommonConfig.PATH, "Test.apk");
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        installIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        installIntent.setDataAndType(Uri.fromFile(localApk),
                "application/vnd.android.package-archive");
        return installIntent;
    }


}
